package com.aier.admin_new.common;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.aier.admin_new.service.IChannelClient;
import com.aier.classes.common.InviteRequestUtil;
import com.aier.classes.entity.ClassParent;
import com.aier.classes.service.IClassUserService;
import com.aier.common.common.AierDateUtil;
import com.aier.common.common.Constant;
import com.aier.common.common.SendSchoolPhoneMessage;
import com.aier.common.dao.ICommonDAO;
import com.aier.common.service.ISysPhoneMessageService;
import com.aier.school.entity.DailyFood;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.entity.mapper.DailyFoodRowMapper;
import com.aier.school.service.ISchoolTeacherService;
import com.google.gson.JsonObject;

/**
 * 定时发布餐饮的定时器
 * @author Wkk300
 */
public class ScheduledTimer {

    @Resource(name = "commonDao")
    private ICommonDAO commonDao;
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;
    @Resource(name = "classUserService")
    private IClassUserService classUserService;
//    @Resource(name="channelClient")
//    private IChannelClient channelClient;
    @Resource(name = "sysPhoneMessageService")
    private ISysPhoneMessageService sysPhoneMessageService;
    

    
	//定时方法
	@SuppressWarnings("unchecked")
	public int  startRun(){
		System.out.println("开始发布餐饮....");
		String nowDate=AierDateUtil.praseDate();
		List<DailyFood> list=(List<DailyFood>) commonDao.list("select df.* ,(select group_concat(img) from dailyfood_img where dfid = df.dfid) as img  "
				+ " from dailyfood df  where df.state = 0 and df.ddate =? ",new Object[]{nowDate}, new DailyFoodRowMapper());
		if(list.size()==0){
				System.out.println("今天没有餐饮....");
			return 0;
			}
		
		for(DailyFood d:list){
			int sid=d.getSid();
			short isMessage=d.getIsmessage();
			String content=d.getContent();
			String img=d.getImg();
			List<List<String>> userPhoneList = new ArrayList<List<String>>();
			List<ClassParent> cpList = classUserService.searchClassParentListSid_account(sid);
			List<SchoolTeacher> stList = schoolTeacherService.searchSchoolTeacherListSid_account(sid);
            
            if(isMessage==1){
            	// 是否发送短信
                for (SchoolTeacher sTeacher : stList) {
                    List<String> upList = new ArrayList<String>();
                    upList.add(sTeacher.getAccount());
                    upList.add(sTeacher.getUid().toString());
                    upList.add("1");
                    userPhoneList.add(upList);
                }
                for (ClassParent sParent : cpList) {
                    List<String> upList = new ArrayList<String>();
                    upList.add(sParent.getAccount());
                    upList.add(sParent.getUid().toString());
                    upList.add("0");
                    userPhoneList.add(upList);
                }   
                // 判断公告的长度,计算每条公告需要发送几条短信
                content = InviteRequestUtil.getFilterMessage(content);
                content = content + "【爱儿360】";
                
                int size = 1;
                
                if (content.length() > Constant.num_70) {
                    int len = content.length();
                    size = ((double)len/(double)Constant.num_67)>(len/Constant.num_67) ? len/Constant.num_67+1 : len/Constant.num_67;
                }
                
                new SendSchoolPhoneMessage(userPhoneList, content, sid,sysPhoneMessageService,1,d.getDfid(),size,0L).start();
                //跟更新学校的短信条数
                sysPhoneMessageService.updateSchoolMessageCount(sid);
            }
            commonDao.update("update dailyfood set state=1 where ddate =? and sid = ? ", new Object[]{nowDate,sid});
            //推送
            JsonObject js =new JsonObject();
            js.addProperty("content", content);
            js.addProperty("img", img);
            js.addProperty("id", d.getDfid());
            MessageBaiduYunSendTransmitter mbysst = new MessageBaiduYunSendTransmitter(AndroidTag.SCHOOLD+sid, "餐饮","您有一条新餐饮#"+js.toString(), AndroidType.SCHOOLDF, 1L, 1);
            mbysst.sendServer();
		}
		System.out.println("发布餐饮结束....");
		return 1;
	}
}
